﻿public class Solution {
    public int MinSubArrayLen(int target, int[] nums)
    {
        int left = 0, right = 0;
        int sum = 0;
        int length = nums.Length;

        bool fail = true;
        
        while (right <= nums.Length)
        {
            if (sum >= target)
            {
                if (right - left < length)
                {
                    length = right - left;
                }
                sum -= nums[left++];
                fail = false;
            }
            else
            {
                if (right == nums.Length)
                    break;
                sum += nums[right++];
            }
        }

        if (fail)
        {
            return 0;
        }
        return length;
    }
    
    public static void Main(string[] args)
    {
        int target = 15;
        int[] nums = [1, 2, 3, 4, 5];
        Solution solution = new Solution();
        int result = solution.MinSubArrayLen(target, nums);
        Console.WriteLine(result);
    }
}